﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using System.Linq;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Media;

namespace LinkED_WP
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            //// 查询所有记录
            LinkED_DataSource.InitialLocalDB();

            // 设置ListBox的数据源
            AllPCItems.ItemsSource = LinkED_DataSource.m_PCResource.PCItems;

            // Call the base method.
            base.OnNavigatedTo(e);
        }

        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {

            base.OnNavigatedFrom(e);

            // 保存数据
            LinkED_DataSource.m_LocalDataBase.SubmitChanges();
        }

        private void Navigate2TouchPadPage(string ip, string name)
        {
            string nvstring = "";

            if (DeviceNetworkInformation.IsNetworkAvailable)
                nvstring = "/TouchPad.xaml?IP=" + ip
                    + "&Port=20000" + "&Name=" + name;
            else
                MessageBox.Show("手机必须连接ZUNE或者WIFI才能连接计算机");
            
            NavigationService.Navigate(new Uri(nvstring, UriKind.Relative));
        }

        static public void InsertIntoDataBase(LinkED_PCItem newPC)
        {
            LinkED_DataSource.m_PCResource.PCItems.Add(newPC);
            LinkED_DataSource.m_LocalDataBase.PCItemTable.InsertOnSubmit(newPC);
        }

        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            PopupCotainer pc = new PopupCotainer(this);
            LinkED_POP pop = new LinkED_POP();
            pc.Show(pop);
        }

        private void ApplicationBarIconButton_Question_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/YourLastAboutDialog;component/AboutPage.xaml", UriKind.Relative));
        }

        private void ApplicationBarIconButton_Feature_Setting(object sender, EventArgs e)
        {
            PopupCotainer pc = new PopupCotainer(this);
            LinkED_POP_SET pop = new LinkED_POP_SET();
            pc.Show(pop);
        }

        private void ApplicationBarMenuItem_DeleteAll_Click(object sender, EventArgs e)
        {
           LinkED_DataSource.EmptyLocalDB();
        }

        private void AllPCItems_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (AllPCItems.SelectedItem != null)
            {
               LinkED_PCItem sel =AllPCItems.SelectedItem as LinkED_PCItem;
               AllPCItems.SelectedIndex = -1;
               Navigate2TouchPadPage(sel.IPAddress, sel.PCName);
            }
        }

        private void ContextMenuItem_Click(object sender, RoutedEventArgs e)
        {
            MenuItem selMenu = sender as MenuItem;
            LinkED_DataSource.DeleteByPCID(Convert.ToInt32(selMenu.Tag));
            return;
        }

        private void ListBox_UnitTest()
        {
            for (int i = 0; i < 10; i++)
            {
                LinkED_PCItem newPC = new LinkED_PCItem { IPAddress = "192.168.1.100", PCName = "xukaijun-PC", TCP = true };
                LinkED_DataSource.m_PCResource.PCItems.Add(newPC);
                LinkED_DataSource.m_LocalDataBase.PCItemTable.InsertOnSubmit(newPC);
            }

        }
    }
}
